<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\admin\model\Ads;

class Webads extends AdminBase
{
	protected function _initialize()
	{
		parent::_initialize();
		$class_type = [1=>'首页轮播',2=>'旅游天地',3=>'金点子'];
		$this->assign('class_type',$class_type);
	}

    public function index()
    {
		$m = new Ads;
		return $this->fetch('index',['list'=>$m->getAll()]);
	}

    public function edit()
    {
        $rows = Ads::get(input('id/d'));
        session('redirect_url',$_SERVER['HTTP_REFERER']);   //记住编辑前的url，修改后调回
        return $this->fetch('edit',['rows'=>$rows]);
    }

	public function save()
    {
        if($this->request->isAjax()){
            $m = new Ads();
            return $m->_save();
        }
	}

	public function flag($id)
    {
        if ($this->request->isAjax()) {
            $m = new Ads;     $data = input('post.');
            $res = $m->allowField(['is_show'])->save($data,['id'=>$id]); //失败返回false
            if($res !== false) $this->success('更新成功');
            else $this->error('请重试');
        }
    }

    public function delete($id = 0, $ids = []){
        if($this->request->isAjax()){
            $id = $ids ? $ids : $id;
            $m = new Ads;
            return $m->_delete($id);
        }
    }
}